package com.cherry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cherry.dao.GoodsMapper;
import com.cherry.entity.Goods;
import com.cherry.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * @create 2020-09-03 9:31 上午
 * @function:
 **/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 根据场次的时间，返回秒杀的商品列表
     * @param time
     * @return
     */
    @Override
    // @Cacheable(cacheNames = "seckill", key = "'times' + #time") //''中是常量
    @Caching(cacheable = {
            @Cacheable(cacheNames = "seckill", key = "'times' + #time")
    })
    public List<Goods> getSecGoodsListByTime(String time) {
        System.out.println("查询数据库了！");
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>().eq("begin_time", time);
        List<Goods> goods = goodsMapper.selectList(queryWrapper);
        return goods;
    }

    /**
     * 修改商品的库存
     * @param gid
     * @param gnumber
     * @return
     */
    @Override
    public int updateGoodsSave(Integer gid, Integer gnumber) {
        return goodsMapper.updateGoodsSave(gid, gnumber);
    }

    /**
     * 重写简单的方法，并添加缓存
     * @param id
     * @return
     */
    @Override
    @Cacheable(cacheNames = "seckill", key = "'goods' + #id")
    public Goods getById(Serializable id) {
        return super.getById(id);
    }
}